package com.richinfo.util;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * request 对象的相关操作
 * 
 * @author zhangtengda
 * @version 1.0
 * @created 2015年5月2日 下午8:25:43
 */
public class GetRequestJsonUtils {

	/**
	 * 定义script的正则表达式
	 */
	private static final String REGEX_SCRIPT = "<script[^>]*?>[\\s\\S]*?<\\/script>";
	/**
	 * 定义style的正则表达式
	 */
	private static final String REGEX_STYLE = "<style[^>]*?>[\\s\\S]*?<\\/style>";
	/**
	 * 定义HTML标签的正则表达式
	 */
	// private static final String REGEX_HTML = "<[^>]+>";
	private static final String REGEX_HTML = "<b><font[^>]*?>[\\s\\S]*?</font></b>";

	private static final String REGEX_HTML_1 = "\\[.*?\\]";
	/**
	 * 定义空格回车换行符
	 */
	private static final String REGEX_SPACE = "\\s*|\t|\r|\n";

	public static String delHTMLTag(String htmlStr) {
		// 过滤script标签
		Pattern p_script = Pattern.compile(REGEX_SCRIPT, Pattern.CASE_INSENSITIVE);
		Matcher m_script = p_script.matcher(htmlStr);
		htmlStr = m_script.replaceAll("");
		// 过滤style标签
		Pattern p_style = Pattern.compile(REGEX_STYLE, Pattern.CASE_INSENSITIVE);
		Matcher m_style = p_style.matcher(htmlStr);
		htmlStr = m_style.replaceAll("");

		// 过滤html标签
		Pattern p_html = Pattern.compile(REGEX_HTML, Pattern.CASE_INSENSITIVE);
		Matcher m_html = p_html.matcher(htmlStr);
		StringBuffer sb = new StringBuffer();
		while (m_html.find()) {
			sb.append(m_html.group(0));
		}
		htmlStr = m_html.replaceAll(getValue(sb.toString()));

		// 过滤空格回车标签
		Pattern p_space = Pattern.compile(REGEX_SPACE, Pattern.CASE_INSENSITIVE);
		Matcher m_space = p_space.matcher(htmlStr);
		htmlStr = m_space.replaceAll("");
		return htmlStr.trim(); // 返回文本字符串
	}

	/**
	 * 正则表达式匹配括号
	 * 
	 * @param str
	 * @return
	 */
	public static String getValue(String str) {
		String regex = REGEX_HTML_1;
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		System.out.println("replace: " + str.replaceAll(regex, "O"));
		while (matcher.find()) {
			str = matcher.group(0);
		}
		return str;
	}

	// public static void main(String [] args){
	// Pattern p_html = Pattern.compile(REGEX_HTML, Pattern.CASE_INSENSITIVE);
	// Matcher m = p_html.matcher("<b><font
	// style='color:red'>[代码题头]</font></b>关于标题包含HTML编码的测试002(无文件详情，点击无效)");
	// StringBuffer sb = new StringBuffer();
	// while (m.find()) {
	// sb.append(m.group(0));
	// }
	//
	// String str = sb.toString();
	// String regex = REGEX_HTML_1;
	// Pattern pattern = Pattern.compile(regex);
	// Matcher matcher = pattern.matcher(str);
	// System.out.println("replace: "+str.replaceAll(regex, "O"));
	// while (matcher.find()) {
	// System.out.println("matcher: " + matcher.group(0));
	// }
	// }

	/***
	 * 获取 request 中 json 字符串的内容
	 * 
	 * @param request
	 * @return : <code>byte[]</code>
	 * @throws IOException
	 */
	public static String getRequestJsonString(HttpServletRequest request) throws IOException {
		String submitMehtod = request.getMethod();
		// GET
		if (submitMehtod.equals("GET")) {
			if(StringUtil.isBlank(request.getQueryString())){
				return null;
			}
			return new String(request.getQueryString().getBytes("iso-8859-1"), "utf-8").replaceAll("%22", "\"");
			// POST
		} else {
			String str = delHTMLTag(repStr(getRequestPostStr(request)));
			// JSONObject json = JSONObject.parseObject(str);
			// for (String jsonKey : json.keySet()) {
			// System.out.println(json.getString(jsonKey));
			// if(AntiSqlInjectionfilter.sqlValidate(json.getString(jsonKey))){
			//
			// throw new IOException("您发送请求中的参数中含有非法字符");
			// }
			// }
			request.setAttribute("req", str);
			return str;
		}
	}

	public static String repStr(String toZhuanyi) {
		toZhuanyi = toZhuanyi.replaceAll("&amp;", "&");
		toZhuanyi = toZhuanyi.replaceAll("&lt;", "<");
		toZhuanyi = toZhuanyi.replaceAll("&gt;", ">");
		// toZhuanyi = toZhuanyi.replaceAll("\\\\\\\\","\\\\");
		// toZhuanyi = toZhuanyi.replaceAll("&quot;","“");
		toZhuanyi = toZhuanyi.replaceAll("&#125;", "%");
		toZhuanyi = toZhuanyi.replaceAll("&#43;", "\\+");
		toZhuanyi = toZhuanyi.replaceAll("&#32;", " ");
		// toZhuanyi = toZhuanyi.replaceAll("&#39;","'");
		return toZhuanyi;
	}

	/**
	 * 描述:获取 post 请求的 byte[] 数组
	 * 
	 * <pre>
	 * 举例：
	 * </pre>
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public static byte[] getRequestPostBytes(HttpServletRequest request) throws IOException {
		int contentLength = request.getContentLength();
		if (contentLength < 0) {
			return null;
		}
		byte buffer[] = new byte[contentLength];
		for (int i = 0; i < contentLength;) {

			int readlen = request.getInputStream().read(buffer, i, contentLength - i);
			if (readlen == -1) {
				break;
			}
			i += readlen;
		}
		return buffer;
	}

	/**
	 * 描述:获取 post 请求内容
	 * 
	 * <pre>
	 * 举例：
	 * </pre>
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public static String getRequestPostStr(HttpServletRequest request) throws IOException {
		byte buffer[] = getRequestPostBytes(request);
		String charEncoding = request.getCharacterEncoding();
		if (charEncoding == null) {
			charEncoding = "UTF-8";
		}
		return new String(buffer, charEncoding);
	}

}